/*
 *  AI.cpp
 *  TractionEdge
 *
 *  Created by Steven Hamilton on 3/09/09.
 *  Copyright 2009 scorch.org. All rights reserved.
 *
 */

#include <iostream>
#include "GameWorld.h"
#include "SquadAI.h"
#include "DisplayManager.h"
#include "defines.h"
#include "Utility.h"

void SquadAI::initAI()
{
	//reset all our monsters
	std::vector<Creature*> creatureList = WORLD.getCreatures(ENEMY, true);

	for (int i=0;i < creatureList.size();i++){
		if (creatureList[i]->isAlive) creatureList[i]->resetAP();
	}
}
	
void SquadAI::processTurn()
{
	Utility tool;
	//iterate through monsters
	std::vector<Creature*> creatureList = WORLD.getCreatures(ENEMY, true);

	for (int i=0;i < creatureList.size();i++){
		Creature * monster = creatureList[i];
		//check it's alive and well
		if (monster->isAlive){
			std::cout << "------Processing-" << monster->name << "------" << std::endl;
			monster->processFOV();
			monster->aiFindTarget();
			Position safePosition;
			if (monster->visibleEnemies.size()>0) {
				safePosition = monster->aiFindSafety();
				std::vector<move_t> moves = tool.findPath(monster->position, safePosition);
				//if we can fire and reac safety then do so
				int moveCost = moves.size();
				if (moveCost+WORLD.firingCost < monster->currentActionPoints)
				 {
					WORLD.attack(monster);
				 }
				monster->aiMoveTo(moves);
			}
		}
		std::cout << "------End---" << monster->name << "------" << std::endl;
	}
}
